<?php

class ControllerFAPurchaseOrder extends Controller {

    private $error = array();

    public function index() {
        $this->language->load('fa/purchase_order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('fa/purchase_order');

        $this->getList();
    }

    public function insert() {
        $this->language->load('fa/purchase_order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('fa/purchase_order');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            print_r($this->request->post); exit();
            $this->model_fa_purchase_order->addOrder($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function update() {
        $this->language->load('fa/purchase_order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('fa/purchase_order');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_fa_purchase_order->editOrder($this->request->get['order_id'], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function delete() {
        $this->language->load('fa/purchase_order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('fa/purchase_order');

        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $order_id) {
                $this->model_fa_purchase_order->deleteOrder($order_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }

    protected function getList() {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'name';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['filter_name'])) {
            $filter_name = $this->request->get['filter_name'];
        } else {
            $filter_name = null;
        }

        if (isset($this->request->get['filter_short_name'])) {
            $filter_short_name = $this->request->get['filter_short_name'];
        } else {
            $filter_short_name = null;
        }

        if (isset($this->request->get['filter_name'])) {
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_short_name'])) {
            $url .= '&filter_short_name=' . urlencode(html_entity_decode($this->request->get['filter_short_name'], ENT_QUOTES, 'UTF-8'));
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('fa/purchase_order/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('fa/purchase_order/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $this->data['orders'] = array();

        $order_total = $this->model_fa_purchase_order->getTotalOrders();

        $results = $this->model_fa_purchase_order->getOrders();
        if($results){
            foreach ($results as $result) {
                //get supplier
                $this->load->model('fa/supplier');
                $supplier = $this->model_fa_supplier->getSupplier($result['supplier_id']);

                $action = array();

                $action[] = array(
                    'text' => $this->language->get('text_edit'),
                    'href' => $this->url->link('fa/purchase_order/update', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL')
                );

                $this->data['orders'][] = array(
                    'order_id' => $result['order_id'],
                    'supplier' => $supplier,
                    'order_code' => $result['order_code'],
                    'note' => $result['note'],
                    'selected' => isset($this->request->post['selected']) && in_array($result['customer_id'], $this->request->post['selected']),
                    'action' => $action
                );
            }
        }
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_name_supplier'] = $this->language->get('column_name_supplier');
        $this->data['column_order_code'] = $this->language->get('column_order_code');
        $this->data['column_note'] = $this->language->get('column_note');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_filter'] = $this->language->get('button_filter');
        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if (isset($this->request->get['filter_name'])) {
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_short_name'])) {
            $url .= '&filter_short_name=' . urlencode(html_entity_decode($this->request->get['filter_short_name'], ENT_QUOTES, 'UTF-8'));
        }

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['sort_name'] = $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
        $this->data['sort_short_name'] = $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . '&sort=short_name' . $url, 'SSL');
        $this->data['sort_sort_order'] = $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, 'SSL');

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $this->data['token'] = $this->session->data['token'];

        $pagination = new Pagination();
        $pagination->total = $order_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $this->data['pagination'] = $pagination->render();

        $this->data['filter_name'] = $filter_name;
        $this->data['filter_short_name'] = $filter_short_name;

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'fa/purchase_order_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    protected function getForm() {
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');

        $this->data['entry_supplier'] = $this->language->get('entry_supplier');
        $this->data['entry_order_date'] = $this->language->get('entry_order_date');
        $this->data['entry_address'] = $this->language->get('entry_address');
        $this->data['entry_note'] = $this->language->get('entry_note');
        $this->data['entry_store'] = $this->language->get('entry_store');
        $this->data['entry_order_code'] = $this->language->get('entry_order_code');

        $this->data['column_purchase_code'] = $this->language->get('column_purchase_code');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_quantity'] = $this->language->get('column_quantity');
        $this->data['column_unit'] = $this->language->get('column_unit');
        $this->data['column_price'] = $this->language->get('column_price');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_add_item'] = $this->language->get('button_add_item');
        $this->data['button_remove'] = $this->language->get('button_remove');
        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');

        $this->data['tab_address'] = $this->language->get('tab_address');
        $this->data['tab_general'] = $this->language->get('tab_general');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['order_id'])) {
            $this->data['action'] = $this->url->link('fa/purchase_order/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $this->data['action'] = $this->url->link('fa/purchase_order/update', 'token=' . $this->session->data['token'] . '&order_id=' . $this->request->get['order_id'] . $url, 'SSL');
        }

        $this->data['cancel'] = $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['order_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $order_info = $this->model_fa_purchase_order->getOrder($this->request->get['order_id']);
        }

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->request->post['supplier_id'])) {
            $this->data['supplier_id'] = $this->request->post['supplier_id'];
        } elseif (!empty($order_info)) {
            $this->data['supplier_id'] = $order_info['supplier_id'];
        } else {
            $this->data['supplier_id'] = '';
        }

        if (isset($this->request->post['address'])) {
            $this->data['address'] = $this->request->post['address'];
        } elseif (!empty($order_info)) {
            $supplier_id = $this->data['supplier_id'];
            if($supplier_id){
                $result = $this->model_fa_purchase_order->getAddressSupplier($supplier_id);
                $this->data['address'] = $result['address'];
            }
        } else {
            $this->data['address'] = '';
        }

        if (isset($this->request->post['order_date'])) {
            $this->data['order_date'] = $this->request->post['order_date'];
        } elseif (!empty($order_info)) {
            $this->data['order_date'] = $order_info['order_date'];
        } else {
            $this->data['order_date'] = '';
        }

        if (isset($this->request->post['store'])) {
            $this->data['store_id'] = $this->request->post['store'];
        } elseif (!empty($order_info)) {
            $this->data['store_id'] = $order_info['store_id'];
        } else {
            $this->data['store_id'] = '';
        }

        if (isset($this->request->post['order_code'])) {
            $this->data['order_code'] = $this->request->post['order_code'];
        } elseif (!empty($order_info)) {
            $this->data['order_code'] = $order_info['order_code'];
        } else {
            $this->data['order_code'] = '';
        }

        if (isset($this->request->post['note'])) {
            $this->data['note'] = $this->request->post['note'];
        } elseif (!empty($order_info)) {
            $this->data['note'] = $order_info['note'];
        } else {
            $this->data['note'] = '';
        }

        if (isset($this->request->post['items'])) {
            $this->data['items'] = $this->request->post['items'];
        } elseif (!empty($order_info)) {
            $this->data['items'] = $order_info['items'];
        } else {
            $this->data['items'] = '';
        }
        
        //supplier
        $this->data['suppliers'] = array();
        $this->load->model('fa/supplier');
        $results = $this->model_fa_supplier->getSuppliers();
        foreach($results as $result){
            $this->data['suppliers'][] = array(
                    'supplier_id' => $result['supplier_id'],
                    'name' => $result['name']
                );
        }

        //store
        $this->data['stores'] = array();
        $this->load->model('fa/store');
        $results = $this->model_fa_store->getStores();
        foreach($results as $result){
            $this->data['stores'][] = array(
                    'store_id' => $result['store_id'],
                    'name' => $result['name']
                );
        }

        $this->template = 'fa/purchase_order_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    protected function validateForm() {
        if (!$this->user->hasPermission('modify', 'fa/purchase_order')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', 'fa/purchase_order')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    //get item
    public function getPurchases(){
        
        $json = array();
        
        if (!empty($this->request->post['supplier_id'])) {
            $supplier_id = $this->request->post['supplier_id'];
        } else {
            $supplier_id = 0;   
        }
        
        $this->load->model('fa/purchase_order');

        $purchase_info = $this->model_fa_purchase_order->getPurchase($supplier_id);
        
         //get address
        
        $result_address = $this->model_fa_purchase_order->getAddressSupplier($supplier_id);
        
        if($result_address){
            $json['address'] = $result_address['address'];
        }

        
        if($purchase_info){
            foreach ($purchase_info as $key => $purchase){
                $item_info = $this->model_fa_purchase_order->getItem($purchase['item_id']);
                
                $json['items'][] = array(
                    'item_id' => $item_info['item_id'],
                    'name' => $item_info['name']
                );
            }
        } 
        $this->response->setOutput(json_encode($json));
    }

    public function ajaxItem(){
        $json = array();
        
        if (!empty($this->request->post['item_id'])) {
            $item_id = $this->request->post['item_id'];
        } else {
            $item_id = 0;   
        }
        
        $this->load->model('fa/item');
        $item_info = $this->model_fa_item->getItem($item_id);
        
        if($item_id){
            $prices = $this->model_fa_item->getItemPurchasePrice($item_id);
            foreach($prices as $price){
                $item_price = $price['price'];
            }
            $json = array(
                'unit' => $item_info['unit'],
                'purchase_code' => $item_info['purchase_code'],
                'price' => $item_price
            );
        }
        $this->response->setOutput(json_encode($json));
    }
}
?>
